import os
import base64

import dashscope
from dashscope.audio.tts_v2 import *



# voice_list=['longyingcui','longyingda','longyingjing','	longyingyan','longyingtian','longyingbing','longsanshu','longxiu_v2','longnan_v2','longhan_v2']
# voice_list_info=['严肃男','开朗高音女','低调冷静女','义正严辞女','温柔甜美女','尖锐强势女','沉稳质感男','博才说书男','睿智青年男','温暖痴情男']
#
# text='更多音频请访问 https://help.aliyun.com/zh/model-studio/cosyvoice-python-sdk 获取'


# os.environ["API_KEY"]="替换为自己的apikey或者在环境中进行配置"
# dashscope.api_key = os.getenv("API_KEY")

model_list=['cosyvoice-v1','cosyvoice-v2','cosyvoice-v3','cosyvoice-v3-plus']#v3和v3-plus需要申请

def creat_mp3(text,model='cosyvoice-v3-plus',voice = "longyingcui",output_file='output.mp3'):
    # 输入参数：需求文本,音色voice(可选),输出文件output_file（可选，默认output.mp3）
    # 返回参数：模型返回的音频的base64字符串

    # 实例化SpeechSynthesizer，并在构造方法中传入模型（model）、音色（voice）等请求参数
    synthesizer = SpeechSynthesizer(model='cosyvoice-v2', voice=voice)
    # 发送待合成文本，获取二进制音频
    audio = synthesizer.call(text)
    print('[Metric] requestId为：{}，首包延迟为：{}毫秒'.format(
        synthesizer.get_last_request_id(),
        synthesizer.get_first_package_delay()))
    # 如果需要将音频转换为base64字符串，可以使用以下代码
    audio_base64 = base64.b64encode(audio).decode('utf-8')

    # # 将音频保存至本地（自选）
    with open(output_file, 'wb') as f:
        f.write(audio)
    return audio_base64


if __name__ == '__main__':
    creat_mp3('Hello,欢迎')